﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AgileBoard.Domain;

namespace AgileBoard.Demo
{
    public class FakeUserManager
    {
        private readonly List<User> userList;

        public FakeUserManager(List<User> users)
        {
            userList = users;
        }

        /// <summary>
        /// Create a user (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public User NewUser()
        {
            var user = new User();

            //System configuration can be used
            user.CreateTime = DateTime.Now;

            user.ValidFrom = DateTime.Now;
            user.ValidTo = Convert.ToDateTime("2999-12-31");
            user.UserName = "default";
            return user;
        }

        /// <summary>
        /// Save user
        /// </summary>
        /// <param name="user">User entity</param>
        /// <returns></returns>
        public User SaveUser(User user)
        {
            Validate(user);
            if (GetUser(user.UserID) != null)
                throw new Exception("ExistingUser");
            user.UserID = Guid.NewGuid();
            userList.Add(user);

            return GetUser(user.UserID);
        }

        /// <summary>
        /// Access to users
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <returns></returns>
        public User GetUser(Guid userId)
        {
            return userList.SingleOrDefault(u => u.UserID == userId);
        }

        /// <summary>
        /// Access to users
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <returns></returns>
        public User GetByName(string userName)
        {
            return userList.SingleOrDefault(u => u.UserName == userName);
        }


        /// <summary>
        /// Users by project
        /// </summary>
        /// <param name="userId">Project ID</param>
        /// <returns></returns>
        public IList GetByProject(Guid projectId)
        {
            return userList.FindAll(u => u.Project != null && u.Project.Name == "demo");
        }

        /// <summary>
        /// Delete User
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <returns></returns>
        public void DeleteUser(Guid userId)
        {
            User user = GetUser(userId);
            if (user == null) throw new Exception("NotFound");
            userList.Remove(user);
        }

        /// <summary>
        /// Update User
        /// </summary>
        /// <param name="user">User Entity</param>
        /// <returns></returns>
        public User UpdateUser(User user)
        {
            return GetUser(user.UserID);
        }

        /// <summary>
        /// Access to users
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return userList;
        }

        private void Validate(User user)
        {
            //TODO throw exception on error.
        }
    }
}